package com.example.administrator.szb.xzyw;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.LoginNewActivity;
import com.example.administrator.szb.adapter.BaseAdapter;
import com.example.administrator.szb.activity.base.BaseActivity;
import com.example.administrator.szb.bean.SQYWBean;
import com.example.administrator.szb.bean.YWBDBean;
import com.example.administrator.szb.tinkerutil.SampleApplicationLike;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.Toasts;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by D N on 2017/10/12.
 */

public class XYDKActivity extends BaseActivity {
    @Bind(R.id.tjzl_text_rzzt_xydk)
    TextView tjzlTextRzztXydk;
    @Bind(R.id.tjzl_ll_rzzt_xydk)
    LinearLayout tjzlLlRzztXydk;
    @Bind(R.id.tjzl_text_xypj_xydk)
    TextView tjzlTextXypjXydk;
    @Bind(R.id.tjzl_ll_xypj_xydk)
    LinearLayout tjzlLlXypjXydk;
    @Bind(R.id.tjzl_text_dbzt_xydk)
    TextView tjzlTextDbztXydk;
    @Bind(R.id.tjzl_ll_dbzt_xydk)
    LinearLayout tjzlLlDbztXydk;
    @Bind(R.id.tjzl_text_schy_xydk)
    TextView tjzlTextSchyXydk;
    @Bind(R.id.tjzl_ll_schy_xydk)
    LinearLayout tjzlLlSchyXydk;
    @Bind(R.id.tjzl_text_zxcs_xydk)
    TextView tjzlTextZxcsXydk;
    @Bind(R.id.tjzl_ll_zxcs_xydk)
    LinearLayout tjzlLlZxcsXydk;
    @Bind(R.id.tjzl_text_sfks_xydk)
    TextView tjzlTextSfksXydk;
    @Bind(R.id.tjzl_ll_sfks_xydk)
    LinearLayout tjzlLlSfksXydk;
    @Bind(R.id.tjzl_text_cf_xydk)
    TextView tjzlTextCfXydk;
    @Bind(R.id.tjzl_ll_cf_xydk)
    LinearLayout tjzlLlCfXydk;
    @Bind(R.id.tjzl_edit_rzje_xydk)
    EditText tjzlEditRzjeXydk;
    @Bind(R.id.tjzl_ll_rzje_xydk)
    LinearLayout tjzlLlRzjeXydk;
    @Bind(R.id.tjzl_edit_rzqx_xydk)
    EditText tjzlEditRzqxXydk;
    @Bind(R.id.tjzl_ll_rzqx_xydk)
    LinearLayout tjzlLlRzqxXydk;
    @Bind(R.id.textView73)
    TextView textView73;
    @Bind(R.id.tjzl_edit_qwll_xydk)
    EditText tjzlEditQwllXydk;
    @Bind(R.id.tjzl_ll_qwll_xydk)
    LinearLayout tjzlLlQwllXydk;
    @Bind(R.id.tjzl_edit_lxr_xydk)
    EditText tjzlEditLxrXydk;
    @Bind(R.id.tjzl_ll_lxr_xydk)
    LinearLayout tjzlLlLxrXydk;
    @Bind(R.id.tjzl_edit_lxfs_xydk)
    EditText tjzlEditLxfsXydk;
    @Bind(R.id.tjzl_ll_lxfs_xydk)
    LinearLayout tjzlLlLxfsXydk;
    @Bind(R.id.tjzl_edit_yq_xydk)
    EditText tjzlEditYqXydk;
    @Bind(R.id.tjzl_test_zs_xydk)
    TextView tjzlTestZsXydk;
    @Bind(R.id.tjzl_button_tj_xydk)
    Button tjzlButtonTjXydk;
    @Bind(R.id.tjzl_text_bmxy_xydk)
    TextView tjzlTextBmxyXydk;

    private int counselor_id = 0;
    YWBDBean ywbdBean;
    ArrayList<String> info = new ArrayList<>();

    int position = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xydk_activity);
        ButterKnife.bind(this);
        SampleApplicationLike.getActivitiesInstance().add(this);
        initToolbar(R.id.xydk_toolbar_include, "信用贷款");

        requestData(getIntent().getIntExtra("id", -1));
        editListenter(tjzlEditYqXydk, tjzlTestZsXydk);//设置字数监听
        hasIntent();
    }
    private void hasIntent() {
        counselor_id = getIntent().getIntExtra("counselor_id",0);
    }
    private void requestData(int id) {
        HashMap<String, String> map = new HashMap<>();
        map.put("code", HttpUtil.getParam());
        map.put("business_id", id + "");
        map.put("counselor_id", counselor_id + "");
        HttpUtil.RequestGsonPost(SampleApplicationLike.getQueueInstance(), Request.Method.POST, YWBDBean.class, URLAddress.URLBASE + URLAddress.URL_BDYW, map, new Response.Listener() {
            @Override
            public void onResponse(Object response) {
                ywbdBean = (YWBDBean) response;
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.toString();
            }
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @OnClick({R.id.tjzl_ll_rzzt_xydk, R.id.tjzl_ll_xypj_xydk, R.id.tjzl_ll_dbzt_xydk, R.id.tjzl_ll_schy_xydk, R.id.tjzl_ll_zxcs_xydk, R.id.tjzl_ll_sfks_xydk, R.id.tjzl_ll_cf_xydk, R.id.tjzl_ll_rzje_xydk, R.id.tjzl_ll_rzqx_xydk, R.id.tjzl_ll_qwll_xydk, R.id.tjzl_button_tj_xydk, R.id.tjzl_text_bmxy_xydk})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tjzl_ll_rzzt_xydk:
                yw_ci_show(0);
                break;
            case R.id.tjzl_ll_xypj_xydk:
                yw_ci_show(1);
                break;
            case R.id.tjzl_ll_dbzt_xydk:
                yw_ci_show(2);
                break;
            case R.id.tjzl_ll_schy_xydk:
                break;
            case R.id.tjzl_ll_zxcs_xydk:
                yw_ci_show(3);
                break;
            case R.id.tjzl_ll_sfks_xydk:
                yw_ci_show(4);
                break;
            case R.id.tjzl_ll_cf_xydk:
                yw_ci_show(5);
                break;
            case R.id.tjzl_ll_rzje_xydk:
                break;
            case R.id.tjzl_ll_rzqx_xydk:
                break;
            case R.id.tjzl_ll_qwll_xydk:
                break;
            case R.id.tjzl_button_tj_xydk:
                if (SampleApplicationLike.getUserloginInstance().getId()<=0) {
                    startActivity(new Intent(this, LoginNewActivity.class));
                }else{
                    requestSubmitData();
                }
                break;
            case R.id.tjzl_text_bmxy_xydk:
                break;
        }
    }


    private void requestSubmitData() {
        boolean flag = checkInfo();
        if (flag) {
            doSubmit();
        } else {

        }
    }

    private void doSubmit() {
        HashMap<String, String> map = new HashMap<>();
        map.put("code", HttpUtil.getParam());
        map.put("business_id", getIntent().getIntExtra("id", -1) + "");
        map.put("counselor_id", counselor_id + "");
        map.put("users_id", ""+SampleApplicationLike.getUserloginInstance().getId());//测试
        map.put("namelink", info.get(info.size() - 3) + "");
        map.put("tel", info.get(info.size() - 2) + "");
        map.put("content", info.get(info.size() - 1) + "");

        for (int i = 0; i < ywbdBean.getData().getBusField().size(); i++) {
            map.put("" + ywbdBean.getData().getBusField().get(i).getId(), info.get(i));
        }

        HttpUtil.RequestGsonPost(SampleApplicationLike.getQueueInstance(), Request.Method.POST, SQYWBean.class, URLAddress.URLBASE + URLAddress.URL_SUBMINT_BDYW, map, new Response.Listener() {
            @Override
            public void onResponse(Object response) {
                if (((SQYWBean) response).getResult() != 1) {
                    Toasts.show(XYDKActivity.this, "" + ((SQYWBean) response).getErr_msg(), Toast.LENGTH_SHORT);
                } else {
                    startActivity(new Intent(XYDKActivity.this, SuccessActivity.class));
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.toString();
            }
        });
    }

    private boolean checkInfo() {
        getInfo();
        return true;
    }

    private void getInfo() {
        info.clear();
        if (TextUtils.isEmpty(tjzlTextRzztXydk.getText().toString())) {
            info.add("");
        } else {
            for (int i = 0; i < ywbdBean.getData().getBusField().get(position).getBuschoices().size(); i++) {
                if (ywbdBean.getData().getBusField().get(position).getBuschoices().get(i).getTitle().equals(tjzlTextRzztXydk.getText().toString())) {
                    info.add(ywbdBean.getData().getBusField().get(position).getBuschoices().get(i).getId() + "");
                }
            }
        }

        position++;
        if (TextUtils.isEmpty(tjzlTextXypjXydk.getText().toString())) {
            info.add("");
        } else {
            for (int i = 0; i < ywbdBean.getData().getBusField().get(position).getBuschoices().size(); i++) {
                if (ywbdBean.getData().getBusField().get(position).getBuschoices().get(i).getTitle().equals(tjzlTextXypjXydk.getText().toString())) {
                    info.add(ywbdBean.getData().getBusField().get(position).getBuschoices().get(i).getId() + "");
                }
            }
        }

        position++;

        if (TextUtils.isEmpty(tjzlTextDbztXydk.getText().toString())) {
            info.add("");
        } else {
            for (int i = 0; i < ywbdBean.getData().getBusField().get(position).getBuschoices().size(); i++) {
                if (ywbdBean.getData().getBusField().get(position).getBuschoices().get(i).getTitle().equals(tjzlTextDbztXydk.getText().toString())) {
                    info.add(ywbdBean.getData().getBusField().get(position).getBuschoices().get(i).getId() + "");
                }
            }
        }
        position++;

        if (TextUtils.isEmpty(tjzlTextZxcsXydk.getText().toString())) {
            info.add("");
        } else {
            for (int i = 0; i < ywbdBean.getData().getBusField().get(position).getBuschoices().size(); i++) {
                if (ywbdBean.getData().getBusField().get(position).getBuschoices().get(i).getTitle().equals(tjzlTextZxcsXydk.getText().toString())) {
                    info.add(ywbdBean.getData().getBusField().get(position).getBuschoices().get(i).getId() + "");
                }
            }
        }
        position++;

        if (TextUtils.isEmpty(tjzlTextSfksXydk.getText().toString())) {
            info.add("");
        } else {
            for (int i = 0; i < ywbdBean.getData().getBusField().get(position).getBuschoices().size(); i++) {
                if (ywbdBean.getData().getBusField().get(position).getBuschoices().get(i).getTitle().equals(tjzlTextSfksXydk.getText().toString())) {
                    info.add(ywbdBean.getData().getBusField().get(position).getBuschoices().get(i).getId() + "");
                }
            }
        }
        position++;

        if (TextUtils.isEmpty(tjzlTextCfXydk.getText().toString())) {
            info.add("");
        } else {
            for (int i = 0; i < ywbdBean.getData().getBusField().get(position).getBuschoices().size(); i++) {
                if (ywbdBean.getData().getBusField().get(position).getBuschoices().get(i).getTitle().equals(tjzlTextCfXydk.getText().toString())) {
                    info.add(ywbdBean.getData().getBusField().get(position).getBuschoices().get(i).getId() + "");
                }
            }
        }


        info.add(tjzlEditRzjeXydk.getText().toString().trim());
        info.add(tjzlEditRzqxXydk.getText().toString().trim());
        info.add(tjzlEditQwllXydk.getText().toString().trim());
        info.add(tjzlEditLxrXydk.getText().toString().trim());
        info.add(tjzlEditLxfsXydk.getText().toString().trim());
        info.add(tjzlEditYqXydk.getText().toString().trim());


    }
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    private void yw_ci_show(final int i) {
        if (ywbdBean != null) {
            BaseAdapter<YWBDBean.DataBean.BusFieldBean.BuschoicesBean> adapter = new BaseAdapter<YWBDBean.DataBean.BusFieldBean.BuschoicesBean>(this, ywbdBean.getData().getBusField().get(i).getBuschoices()) {
                @Override
                public void convert(int position, BaseViewHolder holder, YWBDBean.DataBean.BusFieldBean.BuschoicesBean bean) {
                    ((TextView) holder.getView(R.id.base_tjzl_popuwindow_text_item)).setText(bean.getTitle());
                }

                @Override
                public int getLayoutId(int viewType) {
                    return R.layout.view_popuwindow_base_tjzl_item;
                }
            };
            showPopupwindow(R.layout.view_popuwindow_base_tjzl, R.layout.ggpz_activity, adapter);
            adapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    popWindow.dissmiss();
                    String info_view = ((TextView) view.findViewById(R.id.base_tjzl_popuwindow_text_item)).getText().toString();
                    int id = -1;
                    /*for (BusinessBean.DataBean b : WecomeActivity.businessBean.getData()) {
                        if (info_view.equals(b.getTitle())) {
                            id = b.getId();
                        }
                    }*/
                    switch (i) {
                        case 0:
                            tjzlTextRzztXydk.setText("" + info_view);
                            break;
                        case 1:
                            tjzlTextXypjXydk.setText("" + info_view);
                            break;
                        case 2:
                            tjzlTextDbztXydk.setText("" + info_view);
                            break;
                        case 3:
                            tjzlTextZxcsXydk.setText("" + info_view);
                            break;
                        case 4:
                            tjzlTextSfksXydk.setText("" + info_view);
                            break;
                        case 5:
                            tjzlTextCfXydk.setText("" + info_view);
                            break;
                    }

                }
            });
        }
    }
}
